#coding:utf-8


import json

def initDict(filename):
    M = dict()
    with open(filename,mode='r') as f:
        for line in f:
            z,c = json.loads(line)
            M[z] = c
    return M

def convert(cc, Z,V, filename1, filename2):

    f1 = open(filename1,mode='r')
    f2 = open(filename2,mode='w')

    for line in f1:
        name,vzs,vs,_ = json.loads(line)
        zs = set()
        for _,zz in vzs:
            zs.update(zz)
        zs = list(zs)
        for i in range(cc):
            zs.append('None_%s'%i)
        zs = [ Z[z] for z in zs if Z.get(z) != None]
        if len(zs) <= cc:
            continue
        zs = [str(z) for z in zs]
        vs = [ (v,s) for v,s in vs.items() if V.get(v) != None ]
        vs = [ (V[v],s) for v,s in vs]
        vs = [ str(v)+":"+"%.6f"%s for v,s in vs]
        line = str(name)+"\t"+','.join(vs)+"\t"+','.join(zs)
        f2.write(line+"\n")
    f1.close()
    f2.close()

if __name__ == '__main__':

    print 'Percy Convert'

    Z = initDict('zDict.none.txt')
    V = initDict('vDict.txt')

    filename1 = 'data.2.txt'
    filename2 = 'data.3.txt'

    cc = 5
    convert(cc,Z,V,filename1,filename2)
